package com.wwt.blog.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wwt.blog.mapper.ArticleMapper;
import com.wwt.blog.pojo.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class ThreadService {
    @Async("taskExecutor")  //使用这个线程池
    public void upDateViewCount(Article article, ArticleMapper mapper){
        Article updateArticle = new Article();
        BeanUtils.copyProperties(article,updateArticle);
        updateArticle.setViewCounts(article.getViewCounts()+1);
        LambdaUpdateWrapper<Article> wrapper = new LambdaUpdateWrapper<>();
        wrapper.
                eq(Article::getId,article.getId()).
                eq(Article::getViewCounts,article.getViewCounts());
        mapper.update(updateArticle,wrapper);
        //测试  不会影响主进程
/*        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }
    @Autowired
    private  ArticleMapper articleMapper;
    public void upDateComment(Long  articleId){
        Article article = articleMapper.selectById(articleId);
        article.setCommentCounts(article.getCommentCounts()+1);
        articleMapper.updateById(article);
    }
}
